package indi.kider.server.meter.service;

import com.google.gson.Gson;
import indi.kider.common.constant.BillTypeCons;
import indi.kider.common.model.bill.Meter;
import indi.kider.common.model.meter.*;
import indi.kider.server.meter.mapper.PowerDayReadingMapper;
import indi.kider.server.meter.mapper.RoomMeterMapper;
import indi.kider.server.meter.mapper.WaterDayReadingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.vankid.common.utils.jdk.SocketUtil;

import java.text.SimpleDateFormat;

@Slf4j
@Service
public class DayReadingServiceImpl implements DayReadingService {
    @Autowired
    private PowerDayReadingMapper powerDayReadingMapper;
    @Autowired
    private WaterDayReadingMapper waterDayReadingMapper;
    @Autowired
    private RoomMeterMapper roomMeterMapper;

    /**
     * 获取日冻结数据
     *
     * @param meterReading
     * @return
     */
    @Override
    public MeterReading getDayReading(MeterReading meterReading) throws Exception {
        powerDayReadingMapper.selectAll();
        Integer type = meterReading.getType();
        if (type.equals(BillTypeCons.POWER)) {
            if (powerDayReadingMapper.selectPowerCountLast(meterReading) > 0)
                meterReading = powerDayReadingMapper.selectPowerReadingValue(meterReading);
            else throw new Exception("当前日期电表读数为空！");
        } else {
            if (waterDayReadingMapper.selectWaterCountLast(meterReading) > 0)
                meterReading = waterDayReadingMapper.selectWaterReadingValue(meterReading);
            else throw new Exception("当前日期水表读数为空！");
        }
        return meterReading;
    }

    @Autowired
    private SocketUtil socketUtil;

    /**
     * 发送指令
     *
     * @param instantCommand
     * @return
     * @throws Exception
     */
    @Override
    public CommandResponse sendCommand(InstantCommand instantCommand) throws Exception {
        Gson gosn = new Gson();
        Long no = System.currentTimeMillis();
        log.info(no + "发送命令：" + instantCommand.toString());
        String res = socketUtil.send(gosn.toJson(instantCommand));
        log.info(no + "命令返回：" + res);
        CommandResponse response = gosn.fromJson(res.replace("\u0000", ""), CommandResponse.class);
        if (response.getJl().equals(FAIL))
            throw new Exception(response.getNr());
        return response;
    }

    public static final String FAIL = "fail";
    public static final String SUCCESS = "success";

    /**
     * 抄读水表
     *
     * @param instantCommand
     */
    @Override
    public MeterReading readWaterMeter(InstantCommand instantCommand) throws Exception {
        CommandResponse response = sendCommand(instantCommand);
        WaterInstantReading reading = new Gson().fromJson(response.getNr(), WaterInstantReading.class);
        MeterReading meterReading = reading.getMeterReading();
   /*     meterReading.setCLD(instantCommand.getCld());
        meterReading.setZDDZ(instantCommand.getZddz());*/
        meterReading.setType(BillTypeCons.WATER);
        return meterReading;
    }

    /**
     * 抄读电表
     *
     * @param instantCommand
     */
    @Override
    public MeterReading readPowerMeter(InstantCommand instantCommand) throws Exception {
        CommandResponse response = sendCommand(instantCommand);
        PowerInstantReading reading = new Gson().fromJson(response.getNr(), PowerInstantReading.class);
        MeterReading meterReading = reading.getMeterReading();
       /* meterReading.setCLD(instantCommand.getCld());
        meterReading.setZDDZ(instantCommand.getZddz());*/
        meterReading.setType(BillTypeCons.POWER);
        return meterReading;
    }

    /**
     * 获取表档案（表号获取）
     *
     * @param meter
     * @return
     */
    @Override
    public Meter getMeterDocument(Meter meter) throws Exception {
        RoomMeter query = new RoomMeter();
        query.setDbdz(meter.getMeterCode());
        query.setDzType(meter.getMeterType());
        RoomMeter roomMeter;

        try {
            roomMeter = roomMeterMapper.selectOne(query);
        } catch (Exception e) {
            if (e.getMessage().contains("selectOne()"))
                throw new Exception("表号不唯一！");
            else throw e;
        }
        if (roomMeter != null)
            return roomMeter.getMeter();
        return null;
    }
}
